// pages/feastPage/notOwner/notOwner.js
var netWork = require('../../../utils/netWork.js');
var api = require('../../../utils/api.js');
var isClick = true;
const log = require('../../../utils/logs.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagesUrl: getApp().globalData.imagesUrl,
    topHeight: 0,
    height: 1048,
    ani: 0, //动画是否执行
    pageNum: 1,
    triggered: false, //设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
    feastList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      const {
        SDKVersion = ''
      } = wx.getSystemInfoSync();
      // 获取系统信息
      const that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            addHeight: (res.screenHeight * res.pixelRatio - 1334) / 6
          });
        }
      });
      console.log(that.data.addHeight)
      log.info(SDKVersion)
      const compatibility = util.compareVersion(SDKVersion, '2.2.2');
      log.info(compatibility)
      if (compatibility == -1) {
        wx.showModal({
          content: '您的微信版本过低，请升级后再来吧~',
          confirmText: '我知道了',
          showCancel: false
        });
        return;
      }
    } catch (e) {}
    try {
      // 检测是否可以调用getUpdateManager检查更新
      if (!wx.canIUse("getUpdateManager")) return;
      let updateManager = wx.getUpdateManager();
      // 获取全局唯一的版本更新管理器，用于管理小程序更新
      updateManager.onCheckForUpdate(function (res) {
        log.info(res)
        // 监听向微信后台请求检查更新结果事件
        log.info("是否有新版本：" + res.hasUpdate);
        if (res.hasUpdate) {
          //如果有新版本
          // 小程序有新版本，会主动触发下载操作
          updateManager.onUpdateReady(function () {
            //当新版本下载完成，会进行回调
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，单击确定重启小程序',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启小程序
                  updateManager.applyUpdate();
                }
              }
            })
          })
          // 小程序有新版本，会主动触发下载操作（无需开发者触发）
          updateManager.onUpdateFailed(function () {
            //当新版本下载失败，会进行回调
            wx.showModal({
              title: '提示',
              content: '检查到有新版本，但下载失败，请稍后尝试',
              showCancel: false,
            })
          })
        }
      });
    } catch (e) {
      e
    }
    //高度处理
    let topHeight = wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight');
    this.setData({
      topHeight: topHeight,
      icon: 'common/loading',
    });
    api.judgeNetworkStatus(function (res) {
      if ("none" == res) return;
    })
    this.changeHeight();
    this.queryFeastList();

  },
  lower() {
    this.queryFeastList();
  },
  queryFeastList() {
    const that = this;
    if (that._freshing) return
    that._freshing = true;
    that.isLoading(true, '加载中...')
    netWork.POST_Json('banquet/list', {
      data: {
        userId: wx.getStorageSync('userId'),
        pageNum: that.data.pageNum,

      },
      success: function (res) {
        that.isLoading(false)
        setTimeout(function () {
          that.setData({
            triggered: false,

          })
          that._freshing = false;
        }, 1000);
        if (res.data.retCode == undefined) {
          wx.showToast({
            title: '查询失败，稍后重试',
            icon: 'none'
          })
        }
        if (res.data.retCode == '0000') {
          let list = that.data.feastList;
          if (res.data.data.length > 0) {

            that.setData({
              feastList: list.concat(res.data.data),
              pageNum: that.data.pageNum + 1
            });
          }

        } else {
          wx.showToast({
            title: '查询失败，稍后重试',
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        that.isLoading(false)
        setTimeout(function () {
          that.setData({
            triggered: false,

          })
          that._freshing = false;
        }, 1000);
        wx.showToast({
          title: '查询失败，稍后重试',
          icon: 'none'
        })
      }
    });
  },
  isLoading(isLoading, loadingText) {
    this.setData({
      isLoading: isLoading,
      loadingText: loadingText
    })
  },

  //下拉刷新中奖记录
  async onRefresh() {
    console.log('onfefresh')
    api.judgeNetworkStatus(function (res) {
      if ("none" == res) return;
    })
    this.setData({
      pageNum: 1,
      feastList: []
    });
    this.queryFeastList();


  },
  feastDetail(event) {
    let url = event.currentTarget.dataset.feast.banquetUrl;
    let status = event.currentTarget.dataset.feast.status;
    var str = decodeURIComponent(url);
    var index = str.lastIndexOf("\/");
    str = str.substring(index + 1, str.length);
    console.log(str);
    wx.setStorageSync('qrCodeFeast', str);
    if (status == 0) { //跳转已确认页面
      wx.redirectTo({
        url: '/pages/feastPage/notActivated/notActivated?isBanquetMaster=false',
      })
      return;

    } else if (status == 3 || status == 2 || status == 4) { //跳转扫码激活页面
      wx.redirectTo({
        url: '/pages/feastPage/activated/activated'
      })
      return;
    } else {
      
    }
    
  },
  back() {
    wx.navigateBack({
      delta: 1
    });
  },
  changeHeight() {
    const that = this;
    // if (that.data.height != 527) {
    //   return;
    // }
    let query = wx.createSelectorQuery();
    query.select('.image1').boundingClientRect(rect => {
      let clientWidth = rect.width;
      let ratio = 750 / clientWidth;
      let clientBottom = rect.bottom;
      console.log(ratio);
      let clientHeight = wx.getSystemInfoSync().windowHeight * ratio;

      console.log('屏幕高度' + clientHeight);
      console.log('按钮高度' + clientBottom * ratio);
      let bottom = clientHeight - (clientBottom * ratio) - 48;
      if (bottom >= 0) {
        that.setData({
          height: 1018 + bottom,
        })
      }

      console.log("距离底部的高度：" + bottom)
    }).exec();

  },
  handlerGobackClick() {
    wx.navigateBack({
      delta: 1
    });}
})